
var ipc = require('ipc');

H5x.component("export.export",["common.list"],function(opts){   
    this.container =  opts.container;
    this.opts = opts;
    this.use("render");

    this.use("dai",{
        app_name:"home",
        host:global_api_host
    });

    this.use("ipc");

    this.activitydai = this.dai.create("activity"); 
    this.applydai = this.dai.create("apply"); 
    this.choosendai = this.dai.create("choosen"); 

    this.commondai = this.choosendai;

    this.init(); 

},{
    init:function(){
        this.opts.paystatus = 1;
        this.load_page();

        /*
        var self = this;
        this.choosendai.query({_page:0,_size:300,activityno:1010},function(rs){
            rs.list.forEach(function(item){                
                if(item.applyid == "1audu1lpu"){
                    var details = JSON.parse(item.detail);
                    //console.log(details[0]);
                    console.log(item);                    
                    self.choosendai.update({applyid:item.applyid}, {
                        product:JSON.stringify(details[0])
                    },function(rs){
                        console.log(rs);
                    });
                }
            });
        });
        */


        //剑桥，邱冠昕
        /*
        this.choosendai.update({applyid:"mll8prvt"}, {
            product:'{"__v":0,"_id":"5a0b91dff2a57d30764ef555","_search":"7寸单片|1h7zvo1ev|<p>7寸单片的单价为20元/张，可任意选择，选几张给几张底片。只要选择照片的家长，每们宝宝送一张18寸海报。</p>","amount":0,"content":"<p>7寸单片的单价为20元/张，可任意选择，选几张给几张底片。只要选择照片的家长，每们宝宝送一张18寸海报。</p>","coverurl":"/platform/home/images/default-cover.jpg","ctime":"2017-11-15T01:01:19.271Z","id":"1h7zvo1ev","name":"7寸单片","photodetail":"[{\"name\":\"海报一张\",\"num\":\"1\",\"no\":\"a01\",\"index\":\"0\"},{\"name\":\"7寸单片每张20元\",\"num\":\"999\",\"no\":\"a09\"}]","price":0,"status":9,"utime":"2017-11-15T01:19:06.126Z"}'
        },function(rs){
            console.log(rs);
        });
        */


        /*
        var self = this;
        ["1v98ipwan","sz21f0dj2","sz2mhac2","sz2mhac22","170en6rc7"].forEach(function(aid){
            if(aid && aid.length > 2){
                //console.log(aid);
                self.choosendai.remove({applyid:aid},function(rs){
                    console.log(rs);
                });
            }
           
        });
        */

        /*
        //刘乘宇
        this.choosendai.update({applyid:"nvh1q1sr"}, {
            //"{"a00":"DSC_0346.jpg","a09":"","a01":"DSC_0346.jpg"}"
            choosenmap:JSON.stringify({"a01":"DSC_0346.jpg","a09":"DSC_0346.jpg"})
        },function(rs){
            console.log(rs);
        });

        

        //李佑紫
        //DSC_0821.jpg;DSC_0833.jpg;DSC_0854.jpg
        this.choosendai.update({applyid:"1v9etqk41"}, {
            //"{"a00":"DSC_0346.jpg","a09":"","a01":"DSC_0346.jpg"}"
            choosenmap:JSON.stringify({"a01":"DSC_0821.jpg","a09":"DSC_0821.jpg;DSC_0833.jpg;DSC_0854.jpg"})
        },function(rs){
            console.log(rs);
        });
        */


        
        /*
        var self = this;
        this.activitydai.query({},function(rs){
            self.render("tp-export-export");

            $(".ac-selector").make("ac-selector",{
                aclist:rs.list,
                onchange:function(opts){
                    self.list_apply(opts);
                }
            });
            
        })
        */
        
       // $(".pager-nav").make("pager");
    },

    on_render_list: function(rs) {
        var activityno = $("#select-activity option:selected").attr("data-no");
        this.render("tp-export-lit", {
            activityno:activityno,
            applylist: rs.list,
            getDetail:function(detail){
                var details = [];
                try{
                    detail = JSON.parse(detail);                    
                    var photodetail = JSON.parse(detail.photodetail);
                    photodetail.forEach(function(item) {
                        details.push(item.name+"("+item.num+"张)");
                    });

                }catch(e){
                    
                }
                
                return details.join("+");
            }
        }, function(html) {
            //console.log(html);
            $("#common-item-list").html(html);
        });

        this.bindApplyAction();
    },

    /*
    list_apply:function(opts){
        var self = this;
        var query = {activityid:opts.activityid,status:3};

        if(opts.clazz != "all"){
            query.clazz = opts.clazz;
        }

        self.applydai.query(query,function(rs1){
            self.render("tp-export-lit",{
                activityno:opts.activityno,
                applylist:rs1.list
            },function(html){
                $("#applylist-container").html(html);   
                self.bindApplyAction();             
            });            
        });
    },
    */

    bindApplyAction : function(){
        var self = this;
        this.container.find(".export-choosen").click(function(){

            $("body").mLoading({
                text:"正在导出文件...",
                mask:false
            });

            var applyid = $(this).parent().attr("data-id");
            var dir = $(this).parent().attr("data-dir");
            self.choosendai.query({applyid:applyid},function(rs){
                var choosen = rs.list[0];
                if(!choosen){
                    $.tip("报名信息不正确!");
                    $("body").mLoading("hide");
                    return ;
                }

                self.ipc("copyfiles",{
                    path:dir,
                    choosen:choosen
                    //files:choosen.all
                },function(rs){
                    //console.log(rs);
                    self.load_list();
                    setTimeout(function(){
                        $("body").mLoading("hide");
                    },500);           
                });

            })
        });
       
    }
});